#include "stk_content_type.h"
#include <string.h>

using namespace stk;

ContentType::ContentType()
{
    SetType(STK_FD_NAME_CONTENT_TYPE);
    m_strType    = "";
    m_strSubType = "";
}

ContentType::ContentType(const ContentType &src)
{
    *this = src;
}

ContentType::ContentType(const char *buf)
{
    SetType(STK_FD_NAME_CONTENT_TYPE);
    parse(buf, strlen(buf));
}

ContentType::~ContentType()
{
    clear();
}

void ContentType::clear()
{
    m_strType    = "";
    m_strSubType = "";

    m_listParams.clear();
}


bool ContentType::isvalid() const
{
    return (!m_strType.empty() && !m_strSubType.empty());
}


int32_t ContentType::to_str (string &str)
{
    //str = "";

    if (!isvalid())
    {
        printf("ERRO: <%010d> <%s.ToStr> Invalid Field, \n", GetSession(), GetTypeName());
        return -1;
    }

    str += m_strType;
    str += "/";
    str += m_strSubType;

    m_listParams.to_str(str, false); //如果值为空，则这个参数不合法

    return 0;
}


int32_t ContentType::parse (const char * szBuf, int nBufLen)
{
    clear();

    char *	szSubType = NULL;
    char *	szType_Params = NULL;

    /* How to parse:
    	we'll place the pointers:
    	szSubType      =>  beginning of subtype
    	szType_Params  =>  beginning of params

    	examples:
    	application/multipart ; boundary=
    	^          ^
    */
    while (szBuf && nBufLen > 0 && szBuf[0] == ' ')
    {
        szBuf ++ ;
        nBufLen --;
    }

    if (szBuf == NULL || szBuf[0] == '\0')
    {
        printf("ERRO: <%010d> <%s.Parse> Invalid Buffer\n", GetSession(), GetTypeName());
        return 0;                   /* It's valid to add empty Accept header! */
    }

    szSubType = (char *)strchr (szBuf, '/');
    szType_Params = (char *)strchr (szBuf, ';');

    if (szSubType == NULL)
    {
        printf("ERRO: <%010d> <%s.Parse> Not find '/'\n", GetSession(), GetTypeName());
        return -1;                  /* do we really mind such an error */
    }

    if (szType_Params != NULL)
    {
#if 0

        if (m_listParams.parse_params(string(szType_Params)) != 0)
        {
            printf("ERRO: <%010d> <%s.Parse> Failed to parse params'/'\n", GetSession(), GetTypeName());
            return -1;
        }

#endif
    }
    else
    {
        szType_Params = szSubType + strlen (szSubType);
    }

    if (szSubType - szBuf < 1)
    {
        printf("ERRO: <%010d> <%s.Parse> Invalid subType\n", GetSession(), GetTypeName());
        return -1;
    }

    m_strType.assign(szBuf, szSubType - szBuf);

    if (szType_Params - szSubType < 2)
    {
        printf("ERRO: <%010d> <%s.Parse> Invalid TypeParams or szSubType pos\n", GetSession(), GetTypeName());
        return -1;
    }

    m_strSubType.assign(szSubType + 1, szType_Params - szSubType - 1);

    return 0;                     /* ok */
}


ContentType& ContentType::operator = (const ContentType &src)
{
    clear();

    m_strType    = src.m_strType;
    m_strSubType = src.m_strSubType;
    m_listParams = src.m_listParams;

    MsgField::operator=(src);

    return *this;
}
